const express = require('express');
const axios = require('axios');
const router = express.Router();
const pool = require('../db');

// 更新用户信息
router.post('/update', async (req, res) => {
  try {
    let { openid, avatar, nickname } = req.body;
    console.log('avatar', avatar, nickname)
    // 定义需要过滤的特殊符号的正则表达式
    const specialCharsRegex = /[^a-zA-Z0-9\u4e00-\u9fa5]/g;
    nickname = String(nickname).replace(specialCharsRegex, '');
    // 查询数据库更新用户的头像和昵称
    const query = 'UPDATE sys_user SET avatarUrl = ?, nickname = ? WHERE openid = ?';
    const values = [avatar, nickname, openid];
     
    pool.query(query, values, async (error, results) => {
      console.log('用户表查询接口', results)
      if (error) {
        return res.status(500).json({ message: error.message });
      }
      res.json({
        success: true,
        code: 200,
        data: null,
        errMsg: '成功'
      });
    })
  } catch (error) {
    res.status(500).send()
  }
})

module.exports = router;